package org.compiere.report;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import javax.swing.JButton;
import javax.swing.JFrame;

import org.compiere.apps.ADialog;
import org.compiere.apps.ConfirmPanel;
import org.compiere.apps.IProcessParameter;
import org.compiere.apps.ProcessCtl;
import org.compiere.apps.ProcessParameterPanel;
import org.compiere.apps.Waiting;
import org.compiere.apps.form.FormFrame;
import org.compiere.apps.form.FormPanel;
import org.compiere.grid.ed.VCheckBox;
import org.compiere.grid.ed.VComboBox;
import org.compiere.grid.ed.VDate;
import org.compiere.model.MRole;
import org.compiere.plaf.CompiereColor;
import org.compiere.process.ProcessInfo;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.swing.CLabel;
import org.compiere.swing.CPanel;
import org.compiere.util.ASyncProcess;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;

public class ReportTurnoverStatementOfAccount implements FormPanel, ActionListener, ASyncProcess{
	
	
	/**	Logger			*/
	public static CLogger log = CLogger.getCLogger(ReportTurnoverStatementOfAccount.class);
	/**	FormFrame			*/
	private FormFrame 		m_frame;
	/**	Window No			*/
	public int         	m_WindowNo = 0;
	/** @todo withholding */
	private CPanel panel = new CPanel();
	/**/
	private int AD_Process_ID;
	public boolean         m_isLocked = false;
	private Waiting	m_waiting;
	ASyncProcess m_parent;
	/** */
	ArrayList<String> listQuery = new ArrayList<String>();
	/**/
	private int cntEvent = 1;
	/** */
	private List<ProcessInfoParameter> po = new ArrayList<ProcessInfoParameter>();
	/** */
	private int Subkonto1 = 0;
	private int Subkonto2 = 0;
	private int Subkonto3 = 0;
	private int Analityc_n = 0;
	private boolean Expand1 = false;
	private boolean Expand2 = false;
	private boolean Expand3 = false;
	
	@Override
	public void lockUI(ProcessInfo pi) {
		JFrame frame = Env.getFrame((Container)m_parent);
		m_waiting = new Waiting (frame, Msg.getMsg(Env.getCtx(), "Processing"), false, pi.getEstSeconds());
		panel.setEnabled(false);
		m_isLocked = true;
		
	}
	
	@Override
	public void unlockUI(ProcessInfo pi) {
		if (m_waiting != null)
			m_waiting.dispose();
		m_waiting = null;
		m_frame.dispose();
		
	}

	@Override
	public boolean isUILocked() {
		return m_isLocked;
	}

	@Override
	public void executeASync(ProcessInfo pi) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		//  Generate
		if (e.getSource() == bGenerate)
		{
			//
			AccountInfo mm = (AccountInfo)fieldAccount.getSelectedItem();
			
			//  Prepare Process
			int Proces_ID = AD_Process_ID;	
			ProcessInfo pi = new ProcessInfo (m_frame.getTitle(), Proces_ID);
			pi.setAD_User_ID (Env.getAD_User_ID(Env.getCtx()));
			pi.setAD_Client_ID(Env.getAD_Client_ID(Env.getCtx()));

			
			po.clear();
			po.add(new ProcessInfoParameter("n",Analityc_n,null,"",""));
			po.add(new ProcessInfoParameter("Account_ID",new BigDecimal(mm.mAccount_ID),null,"",""));
			po.add(new ProcessInfoParameter("Date_From",(Timestamp)fieldDateFrom.getValue(),null,"",""));
			po.add(new ProcessInfoParameter("Date_To",(Timestamp)fieldDateTo.getValue(),null,"",""));
			po.add(new ProcessInfoParameter("Subkonto1",new BigDecimal(Subkonto1),null,"",""));
			po.add(new ProcessInfoParameter("Subkonto2",new BigDecimal(Subkonto2),null,"",""));
			po.add(new ProcessInfoParameter("Subkonto3",new BigDecimal(Subkonto3),null,"",""));
			if(Expand1)
				po.add(new ProcessInfoParameter("Expand1",true,null,"",""));
			else
				po.add(new ProcessInfoParameter("Expand1",false,null,"",""));
			if(Expand2)
				po.add(new ProcessInfoParameter("Expand2",true,null,"",""));
			else
				po.add(new ProcessInfoParameter("Expand2",false,null,"",""));
			if(Expand3)
				po.add(new ProcessInfoParameter("Expand3",true,null,"",""));
			else
				po.add(new ProcessInfoParameter("Expand3",false,null,"",""));
			
//			po.add(new ProcessInfoParameter("n",n,null,"",""));
			//
			ProcessInfoParameter[] pp = new ProcessInfoParameter[po.size()];
			po.toArray(pp);
			pi.setParameter(pp);
			
			ProcessParameterPanel pu = new ProcessParameterPanel(m_WindowNo, pi);
			//	Execute Process
			ProcessCtl.process(this, m_WindowNo, (IProcessParameter) pu, pi, null);
			
		}else if(e.getSource() == bCancel){
			dispose();
		}else if(e.getSource() == fieldAccount && fieldAccount.getSelectedItem() != null){
					
			AccountInfo ai = (AccountInfo) fieldAccount.getSelectedItem();
			String chargetype = new String("and type_id in (select c_elementvalue_id from c_elementvalue where value like '6%')");
			int n=1;
			getListQuery();
			for(int i=0; i<listQuery.size(); i++){
				//
				if(checkAnalitics(listQuery.get(i), ai.getAccount_ID())){
					switch (n) {
					case 1:{//--1--субконто1 = Бизнес партнер
						System.out.println(n+" analitics");
						fieldSubkonto1.removeAllItems();
						fieldSubkonto2.removeAllItems();
						fieldSubkonto3.removeAllItems();
						if(cntEvent == 1){
							m_frame.setSize(m_frame.getWidth()+100, m_frame.getHeight()+0);                            
							cntEvent++;
						}
						fieldSubkonto2.setEnabled(false);
						fieldSubkonto3.setEnabled(false);
						checkSubkonto2.setEnabled(false);
						checkSubkonto3.setEnabled(false);
						
						ArrayList<SubkontoInfo> subkontoData1 = getSubkontoData(291);
						for(SubkontoInfo bis :subkontoData1) 
							fieldSubkonto1.addItem(bis);

						break;
						}
					case 2:{//--2--субконто1 = Бизнес партнер, субконто 2 = Договор
						System.out.println(n+" analitics");
						fieldSubkonto1.removeAllItems();
						fieldSubkonto2.removeAllItems();
						fieldSubkonto3.removeAllItems();
						fieldSubkonto1.setEnabled(true);
						fieldSubkonto2.setEnabled(true);
						checkSubkonto1.setEnabled(true);
						checkSubkonto2.setEnabled(true);
						//
						if(cntEvent == 1){
							m_frame.setSize(m_frame.getWidth()+100, m_frame.getHeight()+0);
							cntEvent++;
						}
						fieldSubkonto3.setEnabled(false);
						checkSubkonto3.setEnabled(false);
					
						ArrayList<SubkontoInfo> subkontoData1 = getSubkontoData(291);
						for(SubkontoInfo bis :subkontoData1)
							fieldSubkonto1.addItem(bis);

						ArrayList<SubkontoInfo> subkontoData2 = getSubkontoData(1000034);
						for(SubkontoInfo bis :subkontoData2)
							fieldSubkonto2.addItem(bis);
						break;
						}
					case 3:{ //--3--субконто1 = Форма3, субконто 2 = Расшфровка к форме3
						System.out.println(n+" analitics");
						fieldSubkonto1.removeAllItems();
						fieldSubkonto2.removeAllItems();
						fieldSubkonto3.removeAllItems();
						fieldSubkonto1.setEnabled(true);
						fieldSubkonto2.setEnabled(true);
						checkSubkonto1.setEnabled(true);
						checkSubkonto2.setEnabled(true);
						//
						if(cntEvent == 1){
							m_frame.setSize(m_frame.getWidth()+500, m_frame.getHeight()+0);
							cntEvent++;
						}
						fieldSubkonto3.setEnabled(false);
						checkSubkonto3.setEnabled(false);
						//
						ArrayList<SubkontoInfo> subkontoData1 = getSubkontoData(1000033);
						for(SubkontoInfo bis :subkontoData1)
							fieldSubkonto1.addItem(bis);
						ArrayList<SubkontoInfo> subkontoData2 = getSubkontoData(1000172);
						for(SubkontoInfo bis :subkontoData2)
							fieldSubkonto2.addItem(bis);
						break;
						}
					case 4:{//--4--субконто1 = Тип налогов
						System.out.println(n+" analitics");
						fieldSubkonto1.removeAllItems();
						fieldSubkonto2.removeAllItems();
						fieldSubkonto3.removeAllItems();
						fieldSubkonto1.setEnabled(true);
						checkSubkonto1.setEnabled(true);
						if(cntEvent == 1){
							m_frame.setSize(m_frame.getWidth()+500, m_frame.getHeight()+0);
							cntEvent++;
						}
						fieldSubkonto2.setEnabled(false);
						fieldSubkonto3.setEnabled(false);
						checkSubkonto2.setEnabled(false);
						checkSubkonto3.setEnabled(false);
						
						ArrayList<SubkontoInfo> subkontoData1 = getSubkontoData(53068);
						for(SubkontoInfo bis :subkontoData1)
							fieldSubkonto1.addItem(bis);

						break;
						}
					case 5:{//--5--субконто1 = Тип налогов субконто2 = Код НДС
						System.out.println(n+" analitics");
						fieldSubkonto1.removeAllItems();
						fieldSubkonto2.removeAllItems();
						fieldSubkonto3.removeAllItems();
						fieldSubkonto1.setEnabled(true);
						fieldSubkonto2.setEnabled(true);
						checkSubkonto1.setEnabled(true);
						checkSubkonto2.setEnabled(true);
						if(cntEvent == 1){
							m_frame.setSize(m_frame.getWidth()+500, m_frame.getHeight()+0);
							cntEvent++;
						}
						fieldSubkonto3.setEnabled(false);
						checkSubkonto3.setEnabled(false);
						
						ArrayList<SubkontoInfo> subkontoData1 = getSubkontoData(53068);
						for(SubkontoInfo bis :subkontoData1)
							fieldSubkonto1.addItem(bis);
						ArrayList<SubkontoInfo> subkontoData2 = getSubkontoData(261);
						for(SubkontoInfo bis :subkontoData2)
							fieldSubkonto2.addItem(bis);
						break;
						}
					case 6:{//--6--субконто1 = Тип Статьи, субконто 2 = Статьи доходов
						System.out.println(n+" analitics");
						fieldSubkonto1.removeAllItems();
						fieldSubkonto2.removeAllItems();
						fieldSubkonto3.removeAllItems();
						fieldSubkonto1.setEnabled(true);
						fieldSubkonto2.setEnabled(true);
						checkSubkonto1.setEnabled(true);
						checkSubkonto2.setEnabled(true);
						if(cntEvent == 1){
							m_frame.setSize(m_frame.getWidth()+500, m_frame.getHeight()+0);
							cntEvent++;
						}
						fieldSubkonto3.setEnabled(false);
						checkSubkonto3.setEnabled(false);
						
						ArrayList<SubkontoInfo> subkontoData1 = getSubkontoData(53145);
						for(SubkontoInfo bis :subkontoData1)
							fieldSubkonto1.addItem(bis);
						
						ArrayList<SubkontoInfo> subkontoData2 = getSubkontoData(313," and type_id in (select c_elementvalue_id from c_elementvalue where value like '6%') ");
						for(SubkontoInfo bis :subkontoData2)
							fieldSubkonto2.addItem(bis);
						
						break;
						}
					case 7:{//--7--субконто1 = Тип Статьи, субконто 2 = Статьи доходов, субконто3 = Бизнес партнер
						System.out.println(n+" analitics");
						fieldSubkonto1.removeAllItems();
						fieldSubkonto2.removeAllItems();
						fieldSubkonto3.removeAllItems();
						fieldSubkonto1.setEnabled(true);
						fieldSubkonto2.setEnabled(true);
						fieldSubkonto3.setEnabled(true);
						checkSubkonto1.setEnabled(true);
						checkSubkonto2.setEnabled(true);
						checkSubkonto3.setEnabled(true);
						//
						if(cntEvent == 1){
							m_frame.setSize(m_frame.getWidth()+500, m_frame.getHeight()+0);
							cntEvent++;
						}

						ArrayList<SubkontoInfo> subkontoData1 = getSubkontoData(53145);
						for(SubkontoInfo bis :subkontoData1)
							fieldSubkonto1.addItem(bis);

						ArrayList<SubkontoInfo> subkontoData2 = getSubkontoData(313," and type_id in (select c_elementvalue_id from c_elementvalue where value like '6%') ");
						for(SubkontoInfo bis :subkontoData2)
							fieldSubkonto2.addItem(bis);
						
						ArrayList<SubkontoInfo> subkontoData3 = getSubkontoData(291);
						for(SubkontoInfo bis :subkontoData3)
							fieldSubkonto3.addItem(bis);
						
						break;
						}
					case 8:{//--8--субконто1 = Тип Статьи, субконто2 = Статьи расходов
						System.out.println(n+" analitics");
						fieldSubkonto1.removeAllItems();
						fieldSubkonto2.removeAllItems();
						fieldSubkonto3.removeAllItems();
						fieldSubkonto1.setEnabled(true);
						fieldSubkonto2.setEnabled(true);
						checkSubkonto1.setEnabled(true);
						checkSubkonto2.setEnabled(true);
						if(cntEvent == 1){
							m_frame.setSize(m_frame.getWidth()+500, m_frame.getHeight()+0);
							cntEvent++;
						}
						fieldSubkonto3.setEnabled(false);
						checkSubkonto3.setEnabled(false);
						
						ArrayList<SubkontoInfo> subkontoData1 = getSubkontoData(53145);
						for(SubkontoInfo bis :subkontoData1)
							fieldSubkonto1.addItem(bis);
						
						ArrayList<SubkontoInfo> subkontoData2 = getSubkontoData(313," and type_id in (select c_elementvalue_id from c_elementvalue where value like '7%') ");
						for(SubkontoInfo bis :subkontoData2)
							fieldSubkonto2.addItem(bis);
						
						break;
						}
					case 9:{//--9--субконто1 = Тип Статьи, субконто2 = Статьи расходов, субконто3 = Бизнес партнер
						System.out.println(n+" analitics");
						fieldSubkonto1.removeAllItems();
						fieldSubkonto2.removeAllItems();
						fieldSubkonto3.removeAllItems();
						fieldSubkonto1.setEnabled(true);
						fieldSubkonto2.setEnabled(true);
						fieldSubkonto3.setEnabled(true);
						checkSubkonto1.setEnabled(true);
						checkSubkonto2.setEnabled(true);
						checkSubkonto3.setEnabled(true);
						//
						if(cntEvent == 1){
							m_frame.setSize(m_frame.getWidth()+500, m_frame.getHeight()+0);
							cntEvent++;
						}

						ArrayList<SubkontoInfo> subkontoData1 = getSubkontoData(53145);
						for(SubkontoInfo bis :subkontoData1)
							fieldSubkonto1.addItem(bis);
						
						ArrayList<SubkontoInfo> subkontoData2 = getSubkontoData(313," and type_id in (select c_elementvalue_id from c_elementvalue where value like '7%') ");
						for(SubkontoInfo bis :subkontoData2)
							fieldSubkonto2.addItem(bis);
						
						ArrayList<SubkontoInfo> subkontoData3 = getSubkontoData(291);
						for(SubkontoInfo bis :subkontoData3)
							fieldSubkonto3.addItem(bis);
						
						break;
						}
					case 10:{//--11--субконто1 = Тип Статьи, субконто2 = Статьи расходов, субконто3 = Подразделение
						System.out.println(n+" analitics");
						fieldSubkonto1.removeAllItems();
						fieldSubkonto2.removeAllItems();
						fieldSubkonto3.removeAllItems();
						fieldSubkonto1.setEnabled(true);
						fieldSubkonto2.setEnabled(true);
						fieldSubkonto3.setEnabled(true);
						checkSubkonto1.setEnabled(true);
						checkSubkonto2.setEnabled(true);
						checkSubkonto3.setEnabled(true);
						if(cntEvent == 1){
							m_frame.setSize(m_frame.getWidth()+500, m_frame.getHeight()+0);
							cntEvent++;
						}

						ArrayList<SubkontoInfo> subkontoData1 = getSubkontoData(53145);
						for(SubkontoInfo bis :subkontoData1)
							fieldSubkonto1.addItem(bis);
						
						ArrayList<SubkontoInfo> subkontoData2 = getSubkontoData(313," and type_id in (select c_elementvalue_id from c_elementvalue where value like '7%') ");
						for(SubkontoInfo bis :subkontoData2)
							fieldSubkonto2.addItem(bis);
						
						ArrayList<SubkontoInfo> subkontoData3 = getSubkontoData(53088);
						for(SubkontoInfo bis :subkontoData3)
							fieldSubkonto3.addItem(bis);
						
						break;
						}
					case 11:{//--11--субконто1 = Резерв --субконто2 =Резерв в разрезе  --субконто3 =Бизнес партнер 
						System.out.println(n+" analitics");
						fieldSubkonto1.removeAllItems();
						fieldSubkonto2.removeAllItems();
						fieldSubkonto3.removeAllItems();
						fieldSubkonto1.setEnabled(true);
						fieldSubkonto2.setEnabled(true);
						fieldSubkonto3.setEnabled(true);
						checkSubkonto1.setEnabled(true);
						checkSubkonto2.setEnabled(true);
						checkSubkonto3.setEnabled(true);
						
						ArrayList<SubkontoInfo> subkontoData1 = getSubkontoData(1000200);
						for(SubkontoInfo bis :subkontoData1) 
							fieldSubkonto1.addItem(bis);
						
						ArrayList<SubkontoInfo> subkontoData2 = getSubkontoData(1000201);
						for(SubkontoInfo bis :subkontoData2)
							fieldSubkonto2.addItem(bis);
						
						ArrayList<SubkontoInfo> subkontoData3 = getSubkontoData(291);
						for(SubkontoInfo bis :subkontoData3)
							fieldSubkonto3.addItem(bis);
						
						break;
							}
					}
					if (n < 12) {
						Analityc_n = n;
//						po.clear();
//						po.add(new ProcessInfoParameter("n",n,null,"",""));
					}
				}
			n++;
			}
		}else if (e.getSource() == fieldSubkonto1 && fieldSubkonto1.getSelectedIndex() >= 0){
			SubkontoInfo tmp1 = (SubkontoInfo) fieldSubkonto1.getItemAt(fieldSubkonto1.getSelectedIndex());
			if (tmp1 != null) {
				  Subkonto1 = tmp1.getSubkonto_id();
				  if(Subkonto1!=0) {Expand1 = true; checkSubkonto1.setSelected(true); checkSubkonto1.setEnabled(false); }
				  else {Expand1 = false; checkSubkonto1.setEnabled(true);}
			}
			
		}else if(e.getSource() == fieldSubkonto2 && fieldSubkonto2.getSelectedIndex() >= 0){
			SubkontoInfo tmp2 = (SubkontoInfo) fieldSubkonto2.getItemAt(fieldSubkonto2.getSelectedIndex());
			if (tmp2 != null) {
			  Subkonto2 = tmp2.getSubkonto_id();
			  if(Subkonto2!=0) {Expand2 = true; checkSubkonto2.setSelected(true); checkSubkonto2.setEnabled(false);}
			  else {Expand2 = false; checkSubkonto2.setEnabled(true);}
			}
			
		}else if(e.getSource() == fieldSubkonto3 && fieldSubkonto3.getSelectedIndex() >= 0){
			SubkontoInfo tmp3 = (SubkontoInfo) fieldSubkonto3.getItemAt(fieldSubkonto3.getSelectedIndex());
			if (tmp3 !=null){
			  Subkonto3 = tmp3.getSubkonto_id();
			  if(Subkonto3!=0) {Expand3 = true; checkSubkonto3.setSelected(true); checkSubkonto3.setEnabled(false);}
			  else {Expand3 = false; checkSubkonto3.setEnabled(true);}
			}
		}
		else if(e.getSource() == checkSubkonto1){
			Expand1 = (Boolean)checkSubkonto1.getValue();
		}
		else if(e.getSource() == checkSubkonto2){
			Expand2 = (Boolean)checkSubkonto2.getValue();
		}
		else if(e.getSource() == checkSubkonto3){
			Expand3 = (Boolean)checkSubkonto3.getValue();
		}
	}

	@Override
	public void init(int WindowNo, FormFrame frame) {
		m_WindowNo = WindowNo;
		m_frame = frame;
		try
		{
			jbInit();
			dynInit();
			frame.getContentPane().add(commandPanel, BorderLayout.SOUTH);
			frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
		}
		catch(Exception e)
		{
			log.log(Level.SEVERE, "", e);
		}

	}
	//
	private CPanel mainPanel = new CPanel();
	private BorderLayout mainLayout = new BorderLayout();
	private CPanel PanelAccount = new CPanel();
	private CPanel PanelDate = new CPanel();
	private CPanel PanelSubkonto = new CPanel();
	private GridBagLayout parameterLayout = new GridBagLayout();
	private CPanel commandPanel = new CPanel();
	private FlowLayout commandLayout = new FlowLayout();
	//
	private JButton bGenerate = ConfirmPanel.createOKButton(true);
	private JButton bCancel = ConfirmPanel.createCancelButton(true);
	//
	private CLabel labelAccount = new CLabel();
	private VComboBox fieldAccount = new VComboBox();
	private CLabel labelDateFrom = new CLabel();
	private VDate fieldDateFrom = new VDate();
	private CLabel labelDateTo = new CLabel();
	private VDate fieldDateTo = new VDate();
	private CLabel labelSubkonto1 = new CLabel();
	private VComboBox fieldSubkonto1 = new VComboBox();
	private CLabel labelSubkonto2 = new CLabel();
	private VComboBox fieldSubkonto2 = new VComboBox();
	private CLabel labelSubkonto3 = new CLabel();
	private VComboBox fieldSubkonto3 = new VComboBox();
	private CLabel LChekSubkonto1 = new CLabel();
	private VCheckBox checkSubkonto1 = new VCheckBox();
	private CLabel LChekSubkonto2 = new CLabel();
	private VCheckBox checkSubkonto2 = new VCheckBox();
	private CLabel LChekSubkonto3 = new CLabel();
	private VCheckBox checkSubkonto3 = new VCheckBox();
	
	private void jbInit() {
		CompiereColor.setBackground(panel);
		//
		mainPanel.setLayout(mainLayout);
		PanelAccount.setLayout(parameterLayout);
		PanelDate.setLayout(parameterLayout);
		PanelSubkonto.setLayout(parameterLayout);
		//
		labelAccount.setText(Msg.translate(Env.getCtx(), "Account_ID"));
		labelDateFrom.setText(Msg.translate(Env.getCtx(), "DateFrom"));
		labelDateTo.setText(Msg.translate(Env.getCtx(), "DateTo"));
		labelSubkonto1.setText(Msg.translate(Env.getCtx(), "Subkonto1"));
		labelSubkonto2.setText(Msg.translate(Env.getCtx(), "Subkonto2"));
		labelSubkonto3.setText(Msg.translate(Env.getCtx(), "Subkonto3"));
		LChekSubkonto1.setText(Msg.translate(Env.getCtx(), "Expand1"));
		LChekSubkonto2.setText(Msg.translate(Env.getCtx(), "Expand2"));
		LChekSubkonto3.setText(Msg.translate(Env.getCtx(), "Expand3"));
		fieldAccount.addActionListener(this);
		fieldSubkonto1.addActionListener(this);
		fieldSubkonto2.addActionListener(this);
		fieldSubkonto3.addActionListener(this);
		checkSubkonto1.addActionListener(this);
		checkSubkonto2.addActionListener(this);
		checkSubkonto3.addActionListener(this);
		//
		bGenerate.addActionListener(this);
		bCancel.addActionListener(this);
		mainPanel.add(PanelAccount, BorderLayout.NORTH);
		mainPanel.add(PanelDate, BorderLayout.CENTER);
		mainPanel.add(PanelSubkonto, BorderLayout.PAGE_END);
	    //
		PanelAccount.add(labelAccount,  new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
				,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		PanelAccount.add(fieldAccount,   new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
				,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		//
		PanelDate.add(labelDateFrom,  new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
					,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		PanelDate.add(fieldDateFrom,   new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
					,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		//
		PanelDate.add(labelDateTo,  new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
				,GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
		PanelDate.add(fieldDateTo,   new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0
				,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		//
		
		//
		PanelSubkonto.add(labelSubkonto1,   new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
				,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		PanelSubkonto.add(fieldSubkonto1,   new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0
				,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		//
		PanelSubkonto.add(checkSubkonto1,   new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0
				,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		PanelSubkonto.add(LChekSubkonto1,   new GridBagConstraints(3, 0, 1, 1, 0.0, 0.0
				,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		//
		PanelSubkonto.add(labelSubkonto2,   new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
				,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		PanelSubkonto.add(fieldSubkonto2,   new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0
				,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		//
		PanelSubkonto.add(checkSubkonto2,   new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
				,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		PanelSubkonto.add(LChekSubkonto2,   new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0
				,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		//
		PanelSubkonto.add(labelSubkonto3,   new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
				,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		PanelSubkonto.add(fieldSubkonto3,   new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
				,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		//
		PanelSubkonto.add(checkSubkonto3,   new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
				,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
		PanelSubkonto.add(LChekSubkonto3,   new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0
				,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0));
	
		commandPanel.setLayout(commandLayout);
		commandLayout.setAlignment(FlowLayout.RIGHT);
		commandLayout.setHgap(10);
		commandPanel.add(bCancel, null);
		commandPanel.add(bGenerate, null);
		
	}

	private void dynInit() {
		///load account data
		ArrayList<AccountInfo> accountData = getAccountData();
		for(AccountInfo bis : accountData)
			fieldAccount.addItem(bis);
		//		
		if (fieldAccount.getItemCount() == 0)
			ADialog.error(m_WindowNo, panel, "account.select");
		else
			fieldAccount.setSelectedIndex(0);
		
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		// KNOW THE ORDER TYPE
		String sql_ = "select ad_process_id from ad_process where lower(value) like '%osv%'";
		try
		{
			pstmt = DB.prepareStatement(sql_, null);
			rs = pstmt.executeQuery();
			if (rs.next())
				AD_Process_ID = rs.getInt(1);
		}
		catch (SQLException e)
		{
			log.log(Level.SEVERE, "product", e);
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}	
		//
		fieldDateFrom.setValue(new Timestamp(System.currentTimeMillis()));
		fieldDateTo.setValue(new Timestamp(System.currentTimeMillis()));
	}

	@Override
	public void dispose() {
		if (m_frame != null)
			m_frame.dispose();
		m_frame = null;	
	}
	
	private ArrayList<AccountInfo> getAccountData()
	{
		ArrayList<AccountInfo> data = new ArrayList<AccountInfo>();
		//
		int m_AD_Client_ID = Env.getAD_Client_ID(Env.getCtx());
		//  Bank Account Info
		String sql = MRole.getDefault().addAccessSQL(
			"SELECT c_elementvalue_id, value||' - '||name as name " +
			" FROM c_elementvalue WHERE ad_client_id=? AND isactive='Y' ORDER BY value",
			"c_elementvalue", MRole.SQL_FULLYQUALIFIED, MRole.SQL_RW);
		try
		{
			PreparedStatement pstmt = DB.prepareStatement(sql, null);
			pstmt.setInt(1, m_AD_Client_ID);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next())
			{
				AccountInfo bi = new AccountInfo(rs.getInt(1), rs.getString(2));
				data.add(bi);
			}
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			log.log(Level.SEVERE, sql, e);
		}
		
		return data;
	}
	
	private ArrayList<SubkontoInfo> getSubkontoData(int ad_table_id){
		
		ArrayList<SubkontoInfo> data = new ArrayList<SubkontoInfo>();
		data.add(getDeafultValue(ad_table_id));
		String sql = MRole.getDefault().addAccessSQL(
			"select id,value||'-'||name as name " +   
			" FROM v_analitics WHERE ad_table_id=? AND isactive='Y' ORDER BY value",
			"v_analitics", MRole.SQL_FULLYQUALIFIED, MRole.SQL_RW);
		try
		{
			PreparedStatement pstmt = DB.prepareStatement(sql, null);
			pstmt.setInt(1, ad_table_id);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next())
			{
				SubkontoInfo bi = new SubkontoInfo(rs.getInt(1), rs.getString(2));
				data.add(bi);
			}
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			log.log(Level.SEVERE, sql, e);
		}
		
		return data;
	}

private SubkontoInfo getDeafultValue(int ad_table_id) {
	SubkontoInfo result  = null;
	String sql = "select name from ad_table_trl where ad_language= '" +Env.getContext(Env.getCtx(), "#AD_Language") +"' and ad_table_id = ?";
		try{
			PreparedStatement pstmt = DB.prepareStatement(sql, null);
			pstmt.setInt(1, ad_table_id);
			ResultSet rs = pstmt.executeQuery();
			if(rs.next())
			{
				result = new SubkontoInfo(0, "< "+rs.getString(1)+" >");
		    }
			else {
				result = new SubkontoInfo(-1, "< Выберите субконто >");
			}
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			log.log(Level.SEVERE, sql, e);
		}
	return result;
}
private ArrayList<SubkontoInfo> getSubkontoData(int ad_table_id, String sql1){
		
		ArrayList<SubkontoInfo> data = new ArrayList<SubkontoInfo>();
		data.add(getDeafultValue(ad_table_id));
		//  Subkontos Info
		String sql = MRole.getDefault().addAccessSQL(
			"select id,value||'-'||name as name " +   
			" FROM v_analitics WHERE ad_table_id=? AND isactive='Y' " + sql1 +" ORDER BY value",
			"v_analitics", MRole.SQL_FULLYQUALIFIED, MRole.SQL_RW);

		try
		{
			PreparedStatement pstmt = DB.prepareStatement(sql, null);
			pstmt.setInt(1, ad_table_id);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next())
			{
				SubkontoInfo bi = new SubkontoInfo(rs.getInt(1), rs.getString(2));
				data.add(bi);
			}
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			log.log(Level.SEVERE, sql, e);
		}
		
		return data;
	}

 	private ArrayList<String> getListQuery(){
		//--1--субконто1 = Бизнес партнер 
		listQuery.add("select c_elementvalue_id from c_elementvalue where ad_client_id=? and value in \n "+
						"('1250','125001','125002','125003','125004','312000','321000','322000','339004','339005','339006','344000' \n "+
						",'417004','417005','417006','423000','9','90','9010','901001','901002','9020','902001','902002')" );
		//--2--субконто1 = Бизнес партнер, субконто 2 = Договор
		listQuery.add("select c_elementvalue_id from c_elementvalue where ad_client_id=? and value in \n"+
						"('11','1100','110000','12','1200','121000','122000','123000','124000','126000','127000','1280','128001', \n"+
						"'128002','128003','128004','16','1600','161000','2','20','2000','201000','21','2100','211000','212000', \n"+
						"'213000','214000','216000','217000','2180','218001','218002','218003','218004','22','2200','221000','29','2900','291000' \n"+
						",'3','30','3000','301000','302000','303000','304000','33','3300','331000','332000','333000','334000','336000','337000', \n"+
						"'338000','3390','339001','339002','339003','339007','34','3400','341000','342000','35','3500','351000','353000','354000' \n"+
						",'4','40','4000','401000','402000','41','4100','411000','412000','413000','414000','415000','416000','4170','417001', \n"+
						"'41702','417003','417007','42','4200','421000','422000','44','4400','441000') ");
		//--3--субконто1 = Форма3, субконто 2 = Расшфровка к форме3
		listQuery.add("select c_elementvalue_id from c_elementvalue where ad_client_id=? and value in \n"+
					  "('10','1010','101001','1030','103001','103002','1040','104001','104002','104003','1050','105001','1060','106001','106002'," +
					  "'106003','1070','107000') ");
		//--4--субконто1 = Тип налогов
		listQuery.add("select c_elementvalue_id from c_elementvalue where ad_client_id=? and value in \n"+
				       "('14','1400','141000','143000','31','3100','311000','314000','315000','316000','317000','318000','319000','32','3200','323000','324000') ");
		//--5--субконто1 = Тип налогов субконто2 = Код НДС
		listQuery.add("select c_elementvalue_id from c_elementvalue where ad_client_id=? and value in('142000','313000') ");
		//--6--субконто1 = Тип Статьи, субконто 2 = Статьи доходов
		listQuery.add("select c_elementvalue_id from c_elementvalue where ad_client_id=? and value in \n"+
				       "('6','60','6000','601000','602000','603000','61','6100','611000','612000','613000','614000','615000','616000', \n"+
					   "'62','6200','621000','622000','623000','624000','625000','626000','627000','628000','63','6300','631000') ");
		//--7--субконто1 = Тип Статьи, субконто 2 = Статьи доходов, субконто3 = Бизнес партнер
		listQuery.add("select c_elementvalue_id from c_elementvalue where ad_client_id=? and value in ('64','6400','641000','642000')");
		//--8--субконто1 = Тип Статьи, субконто2 = Статьи расходов
		listQuery.add("select c_elementvalue_id from c_elementvalue where ad_client_id=? and value in \n"+
					  "('7','70','7000','701000','732000','733000','734000','74','7400','741000','742000','743000','745000','746000','747000'," +
					  "'75','7500','751000','77','7700','771000') ");
		//--9--субконто1 = Тип Статьи, субконто2 = Статьи расходов, субконто3 = Бизнес партнер
		listQuery.add("select c_elementvalue_id from c_elementvalue where ad_client_id=? and value in ('76','7600','761000','762000','744000'," +
				"'73','7300','731000') ");
		//--10--субконто1 = Тип Статьи, субконто2 = Статьи расходов, субконто3 = Подразделение
		listQuery.add("select c_elementvalue_id from c_elementvalue where ad_client_id=? and value in ('71','7100','711000','72','7200','721000',"+
				"'8','80','8000','801000','802000','803000','804000')");
		//--11--субконто1 = Бизнес партнер --субконто2 = Резерв --субконто3 = Резерв в разрезе
		listQuery.add("select c_elementvalue_id from c_elementvalue where ad_client_id=? and value in ('343000')");
		return listQuery;
	}
	private boolean checkAnalitics(String sql, int accountId){
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		StringBuffer sql_ = new StringBuffer("select * from (");
		sql_.append(sql);
		sql_.append(") as tab where tab.c_elementvalue_id =?");
		
		try
		{
			pstmt = DB.prepareStatement(sql_.toString(), null);
			pstmt.setInt(1, Env.getAD_Client_ID(Env.getCtx()));
			pstmt.setInt(2, accountId);
			rs = pstmt.executeQuery();
			if (rs.next())
				return true;
		}
		catch (SQLException e)
		{
			log.log(Level.SEVERE, "product", e);
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}
		return false;
	}

	/**************************************************************************
	 *  Account Info
	 */
	public class AccountInfo
	{

		private int mAccount_ID;
		private String mAccountName;
		
		/**
		 * 	AccountInfo
		 *	@param Account_ID
		 *	@param AccountName
		 */
		public AccountInfo (int Account_ID, String AccountName)
		{
			mAccount_ID = Account_ID;
			mAccountName = AccountName;
		}

		/**
		 * 	to String
		 *	@return info
		 */
		public String toString()
		{
			return mAccountName;
		}
		
		public int getAccount_ID(){
			return mAccount_ID;
		}

	}   //  AccountInfo
	
	public class SubkontoInfo{
		private int mSubkonto_id;
		private String mSubkontoName;
		
		public SubkontoInfo(int subkonto_id, String subkontoname) {
			mSubkonto_id = subkonto_id;
			mSubkontoName = subkontoname;
		}

		public String toString(){
			
			return mSubkontoName;
		}
		
		public int getSubkonto_id(){
			
			return mSubkonto_id;
		}
	}

}
